<?php
/*
 * Created on 11.03.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class Controller_Order extends Controller_Base_Admin {
	
	function index($args) {
		$template	= new Template('admin/index.tmpl');
		
		
		$order = new Model_Order();
		
		if($_GET[id]){
			$template->setArgs(array(
				order		=> $order->find(array(id => $_GET[id])),
				order_id	=> $_GET[id]
			));	
		} else {
			$template->setArgs(array(
				orders		=> $order->find(),
				processed	=> $order->find(array(state => 'processed')),
				deleted		=> $order->find(array(state => 'deleted')),
			));		
		}
		
		$template->setArgs(array(
			tabs 		=> $this->tabs->get('order'),  
			menu 		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),

			_user		=> $this->admin,
			is_signed	=> $this->checkSigned('new_orders'),
		));	
		
		return array(
			status		=> 0,
			template	=> $template
		);

	}
	
	function sign($array) {

		$this->admin->sign('new_orders');

		return array(
			status		=> 302,
			location	=> '/admin/order#order-config'
		);
	}
	
	function checkSigned($object) {
		return $this->admin->checkFeed($object) ? true : false; 
	}

	function unsign($array) {

		$this->admin->unSign('new_orders');

		return array(
			status		=> 302,
			location	=> '/admin/order#order-config'
		);
	}
	
	function view($args) {
		$order = new Model_Order();

		$sizes = new Model_Size();
		$colors = new Model_Color();

		$o = $order->find(array(id => $args[0]));
		
		if(isset($_GET['print'])) {	
			$template	= new Template('admin/print.tmpl');
			$template->setArg('file','admin/tabs/order/view.tmpl');
		} else if(isset($_GET['excel'])) {

			require_once site_path.'libs/Spreadsheet/Excel/Writer.php';

			// Creating a workbook
			$workbook = new Spreadsheet_Excel_Writer();
			
			// sending HTTP headers
			$workbook->send("order$args[0].xls");
			
			// Creating a worksheet
			$worksheet =& $workbook->addWorksheet('Order #'.$args[0]);
			
			// The actual data
			$worksheet->write($row, 0, utf8_2_win1251('ФИО'));
			$worksheet->write($row++, 1, utf8_2_win1251($o[0]->realname));
			$worksheet->write($row, 0, 'email');
			$worksheet->write($row++, 1, $o[0]->email);
			$worksheet->write($row, 0, utf8_2_win1251('телефон'));
			$worksheet->write($row++, 1, utf8_2_win1251($o[0]->phone));
			$row++;
			$worksheet->write($row, 0, utf8_2_win1251('Способ оплаты'));
			$worksheet->write($row++, 1, utf8_2_win1251($o[0]->payment ? 'безналичный' : 'наличный'));
			$worksheet->write($row, 0, utf8_2_win1251('Форма доставки'));
			$worksheet->write($row, 1, utf8_2_win1251($o[0]->delivery ? 'транспортной комапнией' : 'самовывоз'));
			
			if($o[0]->delivery) {
				$worksheet->write(++$row, 0, utf8_2_win1251('примечания'));
				$worksheet->write($row, 1, utf8_2_win1251($o[0]->note));
			}
			
			$row++;
			$worksheet->write(++$row, 0, utf8_2_win1251('Наименовнаие'));
			$worksheet->write($row, 1, utf8_2_win1251('Артикул'));
			$worksheet->write($row, 2, utf8_2_win1251('Размеры'));
			$worksheet->write($row, 3, utf8_2_win1251('Цвет'));
			$worksheet->write($row, 4, utf8_2_win1251('Количество коллекций'));
			$worksheet->write($row, 5, utf8_2_win1251('Общее количество'));
			$worksheet->write($row, 6, utf8_2_win1251('Цена'));
			$worksheet->write($row, 7, utf8_2_win1251('Сумма'));
			
			foreach($o as $line)  {
				$worksheet->write(++$row, 0, utf8_2_win1251($line->name));
				$worksheet->write($row, 1, utf8_2_win1251($line->art));
				$worksheet->write($row, 2, utf8_2_win1251($sizes->getStringByMask($line->size)));
				$worksheet->write($row, 3, utf8_2_win1251($colors->getStringByMask($line->color)));
				$worksheet->write($row, 4, $line->collections);
				$worksheet->write($row, 5, $line->quantity);
				$worksheet->write($row, 6, $line->price);
				
				$sum = $line->quantity * $line->price;
				$total += $sum;
				$worksheet->write($row, 7, $sum);
			}
			
			$worksheet->write(++$row, 0, utf8_2_win1251('Итого:'));
			$worksheet->write($row, 7, $total);
			
//			$excel = array(
//				0	=> array (utf8_2_win1251('ФИО'), 	utf8_2_win1251($o[0]->realname)),
//				1	=> array ('email', 					$o[0]->email),
//			);
			
//			foreach ($excel as $row => $value) {
//				foreach ($value as $item) {
//					$worksheet->write(0, 0, utf8_2_win1251('ФИО'));
//				}
//			}
			
//			$worksheet->write(0, 0, utf8_2_win1251('ФИО'));
//			$worksheet->write(0, 1, 'Age');
//			$worksheet->write(1, 0, 'email');
//			$worksheet->write(1, 1, $o[0]->email);
//			$worksheet->write(2, 0, 'Johann Schmidt');
//			$worksheet->write(2, 1, 31);
//			$worksheet->write(3, 0, 'Juan Herrera');
//			$worksheet->write(3, 1, 32);
			
			// Let's send the file
			$workbook->close();	
		
		} 

		if(!isset($template))
			$template	= new Template('admin/index.tmpl');	
		

					
		$template->setArgs(array(
			order		=> $o,
			order_id	=> $args[0],
			tabs		=> $this->tabs->get('order_view'),
			menu 		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
			sizes		=> $sizes->getList(),
			colors		=> $colors->getList(),
		));			
			
		return array(
			status		=> 0,
			template	=> $template
		);			
	}
 	
 	
 	function process ($args) {
 		$order = new Model_Order();
 		$order->process($_GET[id]);

		return array(
			status		=> 302,
			location	=> '/admin/order/#order-proc'
		); 		
 	}
 	
 	function order_delete($args) {
 		$order = new Model_Order();
 		$order->delete($_GET[id]);
 		
		return array(
			status		=> 302,
			location	=> '/admin/order/#order-del'
		); 		
 	}
 } 
?>
